package com.framework.cat.filter;

import com.dianping.cat.Cat;
import com.dianping.cat.CatConstants;
import com.framework.cat.constants.CatHttpConstants;
import com.framework.cat.context.CatContext;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author dongsufeng
 * @date 2021/11/22 14:41
 * @version 1.0
 */
@Activate(group = "consumer")
public class DubboConsumerFilter implements Filter {
	private static final Logger log = LoggerFactory.getLogger(DubboConsumerFilter.class);

	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		if (DubboProviderFilter.EXCLUDES.contains(invoker.getInterface().getName())) {
			return invoker.invoke(invocation);
		}
		Cat.Context ctx = new CatContext();
		Cat.logRemoteCallClient(ctx);
		RpcContext context = RpcContext.getClientAttachment();
		context.setAttachment(CatHttpConstants.CAT_HTTP_HEADER_ROOT_MESSAGE_ID, ctx.getProperty(Cat.Context.ROOT));
		context.setAttachment(CatHttpConstants.CAT_HTTP_HEADER_PARENT_MESSAGE_ID, ctx.getProperty(Cat.Context.PARENT));
		context.setAttachment(CatHttpConstants.CAT_HTTP_HEADER_CHILD_MESSAGE_ID, ctx.getProperty(Cat.Context.CHILD));
		return LogServer.log(invoker,invocation,CatConstants.TYPE_REMOTE_CALL);
	}
}
